

class gameUI extends egret.Sprite {

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.createView, this);
    }
    private frameTime;
    private oiPosition;//操作杆
    private oiPositionBg;//操作杆背景
    private armature;//角色
    private moveBl;//移动动画BL
    private speed=10;//移动速度
    private yanmo;//炎魔
    private atkTime=0;//冷却时间
    private atk;//技能
    private atkT;//技能
    private yanmoAtkBl=false;//炎魔是否攻击
    private myTimer;//炎魔控制器
    private yanmoBody;//炎魔身体
    private crashWidth;//碰撞器大小
    private crashHeight;//碰撞器大小
    private ymAtkArr=[];
    private yanmoAtk=1;
    private createView():void {
        this.ymAtkArr=["skillAttack1","normalAttack","uniqueAttack"];
        this.frameTime = 1 / this.$stage.frameRate;
        //游戏背景
        var bg=new egret.Bitmap(RES.getRes("gameBg_jpg"));
       this.addChild(bg);

       //制作炎魔
        var yanmosData = RES.getRes( "yanmo_json" );  
        var yanmoText = RES.getRes( "yanmoTexture_json" );  
        var yanmoTexture = RES.getRes( "yanmoTexture_png" );
        var yanmoFactory:dragonBones.EgretFactory = new dragonBones.EgretFactory();  
        yanmoFactory.addDragonBonesData(dragonBones.DataParser.parseDragonBonesData(yanmosData));  
        yanmoFactory.addTextureAtlas(new dragonBones.EgretTextureAtlas(yanmoTexture,yanmoText));
        this.yanmo= yanmoFactory.buildArmature("armatureName");
        this.addChild(this.yanmo.display);
        this.yanmo.display.scaleX = -.5;
        this.yanmo.display.scaleY = .5;
        this.yanmo.display.x = 800;
        this.yanmo.display.y =500;
        dragonBones.WorldClock.clock.add(this.yanmo);
        this.yanmo.animation.play("steady",0);
        this.yanmo.getBones()[9].visible=false;
        this.yanmoBody=this.yanmo.getBones()[14];
       //制作猪脚
       var dragonbonesData = RES.getRes( "SwordsMan_json" );  
        var textureData = RES.getRes( "texture_json" );  
        var texture = RES.getRes( "texture_png" );
        var dragonbonesFactory:dragonBones.EgretFactory = new dragonBones.EgretFactory();  
        dragonbonesFactory.addDragonBonesData(dragonBones.DataParser.parseDragonBonesData(dragonbonesData));  
        dragonbonesFactory.addTextureAtlas(new dragonBones.EgretTextureAtlas(texture,textureData));
        var armature: dragonBones.Armature = dragonbonesFactory.buildArmature("Swordsman");
        this.addChild(armature.display);
        armature.display.x = 200;
        
        armature.display.y = 500;
        armature.display.scaleX = 0.3;
        armature.display.scaleY = 0.3;
        dragonBones.WorldClock.clock.add(armature);
        armature.animation.play("steady",0);
        egret.startTick(this.onTicker, this);
        this.armature=armature;
        this.crashWidth = 200;
        this.crashHeight = 50;

        
        
            //上下左右操作
            this.oiPositionBg=drawCircle(150,520,100,0xd1d4df);//位置操作背景
            this.addChild(this.oiPositionBg);
            this.oiPositionBg.alpha=.3;

            this.oiPosition=drawCircle(150,520,50,0xd1d4df);//位置操作背景
            this.addChild(this.oiPosition);
            this.oiPosition.alpha=.5;

        //操作移动
        this.oiPositionBg.touchEnabled=true;
        this.touchEnabled=true;
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,function(){
            this.oiPositionBg.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.oiArm,this);
            this.moveBl=true;
            
        },this);

        this.addEventListener(egret.TouchEvent.TOUCH_END,function(){
            this.oiPositionBg.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.oiArm,this);
            this.armature.animation.play("steady",0);
            this.oiPosition.x=0;
            this.oiPosition.y=0;
            this.moveBl=false;
        },this);
        //攻击按钮
        this.atk=createBitmap("atk (2)_png",950,490);
        this.atk.scaleX=2;
        this.atk.scaleY=2;
        this.addChild(this.atk);
        //显示冷却时间
        this.atkT=createTextFiled("",1000,540,50,0xffffff);
        this.addChild(this.atkT);
        //攻击事件
        this.atk.touchEnabled=true;
        this.atk.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
            //判断是否攻击
            if(this.atkTime==0){
                this.touchEnabled=false;
                this.atk.touchEnabled=false;
                this.atkTime=5;
                this.atk.alpha=.5;
                this.armature.animation.play("attack1_+1",1);
                this.atkT.text=this.atkTime;
                
            }
            
        },this)
        //添加游戏的帧事件
         this.addEventListener( egret.Event.ENTER_FRAME, this.loop, this ); 
         this.myTimer = new egret.Timer(1000);
         this.myTimer.addEventListener(egret.TimerEvent.TIMER, this.onTimer, this);
         this.myTimer.start();
    }
    private _time:number;

    private onTicker(timeStamp:number) {

        if(!this._time) {
            this._time = timeStamp;
        }

        var now = timeStamp;
        var pass = now - this._time;
        this._time = now;

        dragonBones.WorldClock.clock.advanceTime(-1);

        return false;
    } 

    //移动操作杆
    private oiArm(e){
        this.oiPosition.x=e.$stageX-120;
        this.oiPosition.y=e.$stageY-520;
        if(this.moveBl){
            this.armature.animation.play("walk",0);
            this.moveBl=false;
        }
        
    }
    //游戏主循环
    private loop(){
                    
       //向右移动
            if(this.oiPosition.x>10){
                if(this.armature.display.x>=1136){
                    this.armature.display.x=1136;
                }else{
                    //检测碰撞让他不移动
                    this.armature.display.scaleX = 0.3;//转身
                    var isCRight=isCrash(this.armature.display,this.yanmo.display,50,50,200,80,
                this.armature.display.x+80,this.armature.display.y,this.yanmo.display.x,this.yanmo.display.y);
                    if(!isCRight){
                        this.armature.display.x+=this.speed;
                    }
                   
                }
                
            }
            //向左
            if(this.oiPosition.x<-10){
                if(this.armature.display.x<=0){
                    this.armature.display.x=0;
                }else{
                    //检测碰撞让他不移动
                    this.armature.display.scaleX = -0.3;//转身
                    var isCLeft=isCrash(this.armature.display,this.yanmo.display,20,50,200,80,
                this.armature.display.x-20,this.armature.display.y,this.yanmo.display.x,this.yanmo.display.y);
                    if(!isCLeft){
                        this.armature.display.x-=this.speed;
                    }
                }
            }
            //向上
            if(this.oiPosition.y<-10){
                if(this.armature.display.y<=380){
                    this.armature.display.y=380;
                }else{
                    this.armature.display.y-=this.speed*.5;
                }
                
            }
            //向下
            if(this.oiPosition.y>10){
                if(this.armature.display.y>=640){
                    this.armature.display.y=640;
                }else{
                    this.armature.display.y+=this.speed*.5;
                }
            }
            //技能冷却
            this.atkTime-=this.frameTime;
            this.atkT.text=Math.floor(this.atkTime);
            if(this.atkTime<=0){
                this.atkTime=0;
                this.atk.alpha=1;
                this.atkT.text="";
                this.atk.touchEnabled=true;
                this.touchEnabled=true;   
            }
        
          

     //攻击
     if(this.armature.animation.getState("attack1_+1")){
         //判断角色面向，设置攻击范围
         if(this.armature.display.scaleX>0){
             var isCatk=isCrash(this.armature.display,this.yanmo.display,300,100,200,80,
            this.armature.display.x+150,this.armature.display.y,this.yanmo.display.x,this.yanmo.display.y);
         }else{
             var isCatk=isCrash(this.armature.display,this.yanmo.display,300,100,200,80,
            this.armature.display.x-300,this.armature.display.y,this.yanmo.display.x,this.yanmo.display.y);
         }  
            
            if(isCatk){
                if(this.armature.animation.getState("attack1_+1").isPlaying){
                    if (this.yanmo.animation.lastAnimationName != "hit") {
                        this.yanmo.animation.play("hit",0);
                    }
                }else{
                    
                    if (this.yanmo.animation.lastAnimationName != this.ymAtkArr[this.yanmoAtk]) {
                        this.yanmo.animation.play(this.ymAtkArr[this.yanmoAtk],0);
                    }
                    if(this.yanmo.animation.getState(this.ymAtkArr[this.yanmoAtk]).isPlaying){
                        this.armature.animation.play("jump");
                            this.yanmoAtk=Math.floor(Math.random()*3);
                    }
                   
                }
                   
            }
     }
     //炎魔攻击碰撞
    
    //  if(this.yanmo.animation.getState("skillAttack1")){
    //         var isCatkYanmo=isCrash(this.armature.display,this.yanmo.display,150,50,400,80,
    //     this.armature.display.x,this.armature.display.y,this.yanmo.display.x-200,this.yanmo.display.y);
    //      if(isCatkYanmo){
    //          if(this.yanmo.animation.getState("skillAttack1").isPlaying){
    //                 this.armature.animation.play("jump");
    //         }
    //      }else{
    //          if (this.armature.animation.lastAnimationName != "steady") {
    //                     this.armature.animation.play("steady",0);
    //                 }
    //      }
            
    //  }
            //控制炎魔
        
        if(!isCatk){
            var dis=Math.abs(this.yanmo.display.x-this.armature.display.x);
            if(dis>=500||dis<=150){
                this.yanmo.display.x-=0;
                if(dis<=150){
                    
                    if (this.yanmo.animation.lastAnimationName != this.ymAtkArr[this.yanmoAtk]) {
                        this.yanmo.animation.play(this.ymAtkArr[this.yanmoAtk],0);
                    }
                }
                if(dis>=500){
                    if (this.yanmo.animation.lastAnimationName != "steady") {
                        this.yanmo.animation.play("steady");
                    }
                }
            }else{
                if((this.yanmo.display.x-this.armature.display.x)>1){
                    if (this.yanmo.animation.lastAnimationName != "run") {
                        this.yanmo.animation.play("run");
                    }
                    this.yanmo.display.x-=this.speed*.2;
                    this.yanmo.display.scaleX = -.5;
                }else{
                    if (this.yanmo.animation.lastAnimationName != "run") {
                        this.yanmo.animation.play("run");
                    }
                    this.yanmo.display.scaleX = .5;
                    this.yanmo.display.x+=this.speed*.2;
                }
                
            }
            if(this.yanmo.display.y==this.armature.display.y){
                    this.yanmo.display.y-=0;
                    
                }else{
                    if((this.yanmo.display.y-this.armature.display.y)>0){
                        this.yanmo.display.y-=this.speed*.15;
                        
                    }else{
                        
                        this.yanmo.display.y+=this.speed*.15;
                    }
                }

                
        }

      
            
        
    }

    //炎魔控制器
    private onTimer(){

        if(this.yanmoAtkBl){
            
            
            
        }
    }
}
